package cn.com.inossem.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.InputType;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.foxykeep.datadroid.requestmanager.Request;

import java.util.Timer;
import java.util.TimerTask;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import cn.com.inossem.base.RequestActivity;
import cn.com.inossem.requestmanager.WooRequestFactory;
import cn.com.inossem.utils.ActivityStackControlUtil;
import cn.com.inossem.utils.ToastUtil;

/**
 * 检索向导
 * <p>
 * <p>
 * detailed comment
 *
 * @author szc 2015-8-24
 * @see
 * @since 1.0
 */
public class NameActivity extends RequestActivity {

	TextView titlename, addtext;

	EditText edittext;

	ImageView back;

	int type = 0;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// setContentView(R.layout.main_tab_layout);
		initView();

	}

	@Override
	public void onDestroy() {
		ActivityStackControlUtil.remove(this);
		super.onDestroy();
	}

	private void initView() {
		type = getIntent().getIntExtra("type", 0);

		titlename = (TextView) this.findViewById(R.id.titlename);
		edittext = (EditText) this.findViewById(R.id.edittext);
		back = (ImageView) this.findViewById(R.id.back);
		back.setOnClickListener(this);

		switch (type) { // resultCode为回传的标记，我在B中回传的是RESULT_OK
			case 100:// 昵称
				titlename.setText("修改昵称");
				break;
			case 101:// 真实姓名
				titlename.setText("修改真实姓名");
				break;
			case 102:// 邮箱
				titlename.setText("修改邮箱");
				break;
			case 106:// 工作年限
				titlename.setText("工作年限");
				edittext.setInputType(InputType.TYPE_CLASS_NUMBER);
				break;
			case 107:// sap年限
				titlename.setText("咨询从业年限");
				edittext.setInputType(InputType.TYPE_CLASS_NUMBER);
				break;
			case 108:
				titlename.setText("入职年限");
				edittext.setInputType(InputType.TYPE_CLASS_NUMBER);
				break;
			case 109:
				titlename.setText("银行账号");
				edittext.setInputType(InputType.TYPE_CLASS_NUMBER);
				break;
			case 110:
				titlename.setText("职位");
				break;
			case 200:// 项目名称
				titlename.setText("项目名称");
				break;
		}

		addtext = (TextView) this.findViewById(R.id.addtext);
		addtext.setOnClickListener(this);

		edittext.setFocusable(true);

		edittext.setFocusableInTouchMode(true);

		edittext.requestFocus();

		Timer timer = new Timer();

		timer.schedule(new TimerTask()

		               {

			               public void run()

			               {

				               InputMethodManager inputManager =

						               (InputMethodManager) edittext.getContext().getSystemService(
								               Context.INPUT_METHOD_SERVICE);

				               inputManager.showSoftInput(edittext, 0);
			               }

		               },

				600);

	}

	@Override
	public Request getInitialRequest() {
		return null;

	}

	@Override
	public void onRequestSucess(Request request, Bundle bundle) {

	}

	@Override
	public void onRequestError(int exceptionType) {

	}

	public int getContentViewId() {
		return R.layout.name_layout;

	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
			case R.id.back:
				finish();
				break;
			case R.id.addtext:
				if (edittext.getText().toString().length() > 0) {
					Intent intent = new Intent();
					// 上传 回调
					switch (type) { // resultCode为回传的标记，我在B中回传的是RESULT_OK
						case 100:// 昵称
							// netContent("nickname", edittext.getText().toString());
							launchRequest(WooRequestFactory.updatePersonal("nickname",
									edittext.getText().toString()));
							intent.putExtra("name", edittext.getText().toString());
							break;
						case 101:// 真实姓名
							// netContent("truename", edittext.getText().toString());
							launchRequest(WooRequestFactory.updatePersonal("truename",
									edittext.getText().toString()));
							intent.putExtra("name", edittext.getText().toString());
							break;
						case 102:// 邮箱
							// netContent("email", edittext.getText().toString());
							if (isEmail(edittext.getText().toString())) {
								launchRequest(WooRequestFactory.updatePersonal("email",
										edittext.getText().toString()));
								intent.putExtra("name", edittext.getText().toString());
							} else {
								Toast.makeText(this, "邮箱格式有误!", Toast.LENGTH_LONG)
										.show();
								return;
							}
							break;
						case 106:
							launchRequest(WooRequestFactory.updatePersonal("workYear",
									edittext.getText().toString()));
							intent.putExtra("name", edittext.getText().toString());
							break;
						case 107:
							launchRequest(WooRequestFactory.updatePersonal("sapYear",
									edittext.getText().toString()));
							intent.putExtra("name", edittext.getText().toString());
							break;
						case 108:
							if (Integer.parseInt(edittext.getText().toString()) >= 100) {
								Toast.makeText(this, "太大了!", Toast.LENGTH_LONG).show();
								return;
							}
							intent.putExtra("name", edittext.getText().toString() + "年");
							break;
						case 109:
							if (matchLuhn(edittext.getText().toString())) {
								launchRequest(WooRequestFactory.updatePersonal("bankAccount", edittext.getText().toString()));
								intent.putExtra("name", edittext.getText().toString());
							} else {
								ToastUtil.show(this, "请填写正确的银行卡卡号");
								return;
							}
							break;
						case 200:
							System.out.println("edittext.getText().toString():"
									+ edittext.getText().toString());
							intent.putExtra("name", edittext.getText().toString());
							break;
					}
					setResult(type, intent);
					finish();
				} else {
					Toast.makeText(this, "不能为空！", Toast.LENGTH_LONG).show();
				}
				break;
			default:
				break;
		}
	}

	// 判断email格式是否正确
	public boolean isEmail(String email) {
		String str = "^([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$";
		Pattern p = Pattern.compile(str);
		Matcher m = p.matcher(email);

		return m.matches();
	}

	// public void netContent(String key, String value) {
	// NetworkConnect connection = new NetworkConnect(WSConfig.LOCAL_URL
	// + "userInfo/updateUserInfo");
	// connection.setPostText("{\"" + key + "\":\"" + value + "\"}");
	// System.out.println("{\"" + key + "\":\"" + value + "\"}");
	// String body = connection.execute();
	// System.out.print(key + ":" + body);
	// }

	/**
	 * 匹配Luhn算法：可用于检测银行卡卡号
	 *
	 * @param cardNo
	 * @return
	 */
	public boolean matchLuhn(String cardNo) {
		int[] cardNoArr = new int[cardNo.length()];
		for (int i = 0; i < cardNo.length(); i++) {
			cardNoArr[i] = Integer.valueOf(String.valueOf(cardNo.charAt(i)));
		}
		for (int i = cardNoArr.length - 2; i >= 0; i -= 2) {
			cardNoArr[i] <<= 1;
			cardNoArr[i] = cardNoArr[i] / 10 + cardNoArr[i] % 10;
		}
		int sum = 0;
		for (int i = 0; i < cardNoArr.length; i++) {
			sum += cardNoArr[i];
		}
		return sum % 10 == 0;
	}
}
